﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimChiTietNhanVien : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        private FormTimKiemPhieuNhap frmTimKiemPhieuNhap;
        private FormTimKiemChungTu frmTimKiemChungTu;
        private FormTimKiemPhieuThu frmTimPhieuThu;

        public FormTimChiTietNhanVien()
        {           
            InitializeComponent();
        }

        public FormTimChiTietNhanVien(FormTimKiemPhieuNhap frm)
        {
            frmTimKiemPhieuNhap = frm;
            InitializeComponent();
        }

        public FormTimChiTietNhanVien(FormTimKiemChungTu frm)
        {
            frmTimKiemChungTu = frm;
            InitializeComponent();
        }

        public FormTimChiTietNhanVien(FormTimKiemPhieuThu frm)
        {
            frmTimPhieuThu = frm;
            InitializeComponent();
        }

        private void btnTimMH_Click(object sender, EventArgs e)
        {
            var danhsachnhanvientk = (from m in db.NHANVIENs
                                       where m.TenNhanVien.StartsWith(this.txtTenNV.Text)
                                       select m).ToList();
            this.nHANVIENBindingSource.DataSource = danhsachnhanvientk;

        }

        private void btnThoatCTKh_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnhuyCTKh_Click(object sender, EventArgs e)
        {
            this.txtTenNV.ResetText();
            this.txtTenNV.Focus();
        }

        private void FormTimChiTietNhanVien_Load(object sender, EventArgs e)
        {
            this.dataGridViewCTNV.AutoGenerateColumns = false;
            var chitietnhanvien = (from ctnv in db.NHANVIENs
                                   orderby ctnv.MaNhanVien ascending
                                   select ctnv
                                       ).ToList();
            this.nHANVIENBindingSource.DataSource = chitietnhanvien;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var nhanvien = this.nHANVIENBindingSource.Current as NHANVIEN;
            if (nhanvien != null)
            {               
                // this.frmLapChungTu.txtMaKH.Text = khachhang.MaKhachHang;
                if ( frmTimKiemPhieuNhap!= null)
                {                   
                    this.DialogResult = DialogResult.OK;
                    this.frmTimKiemPhieuNhap.comboBoxNV.Text = nhanvien.MaNhanVien;
                    this.Close();
                }

                if (frmTimKiemChungTu != null)
                {                    
                    this.DialogResult = DialogResult.OK;
                    this.frmTimKiemChungTu.comboBoxNV.Text = nhanvien.MaNhanVien;
                    this.Close();
                }

                if (frmTimPhieuThu!=null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmTimPhieuThu.comboBoxNV.Text = nhanvien.MaNhanVien;
                    this.Close();
                }

            }
            else
            {
                MessageBox.Show("Chưa chọn nhân viên");
            }
        }

        private void btnThemCTKH_Click(object sender, EventArgs e)
        {
            var kq = new FormChiTietNhanVien(this).ShowDialog();
            if (kq == DialogResult.OK)
            {
                MessageBox.Show("Đã thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

    }
}
